# -*- coding: utf-8 -*-
{   xtype:'editorgrid',   
	id : '${componentId}',
    ds: ${ ds.display(value_for(ds), **args_for(ds)) },
    cm: ${ cm.display(value_for(cm), **args_for(cm)) },
    title:'${title}',
    width:${width},
    autoHeight : true,
    maxHeight:${height},
    autoExpandColumn:'${autoExpandColumn}',
    frame:${str(frame).lower()},
    iconCls: 'icon-grid',
    clicksToEdit:${clicksToEdit},
    saveUrl : '${saveUrl}',
 	viewConfig: {
        forceFit:true
    },
    %if len(plugins)>0:
    	plugins :[
    		<%sepplugin=''%>
    	%for plugin in plugins:
    		${sepplugin+plugin}
    		<%sepplugin=','%>
        %endfor
        ],
    %endif   
    %if selModel != None:
    	selModel : new Ext.grid.CellSelectionModel(${ selModel.display(value_for(selModel), **args_for(selModel)) }),
    %endif
    tbar: [
    %if not inFieldSet:
    {
        text: 'Save',
        handleSuccess: function(){		    myds.commitChanges();},
        handleFailure: function(){		    },        
        handler : function(button){
		    myds = Ext.getCmp('${componentId}').store;
		    recordCount = myds.getCount();
		    rows = myds.getRange(0,recordCount);
		    var updated_data=[]
		    var y=0;
		    
		    for (x=0; x < recordCount; x++)
		    {  
		    	if(rows[x].dirty || rows[x].modified!=null){
		    		updated_data[y] = rows[x].data;
		    		y++;
		    	}
		    }       
		    json_data = Ext.encode(updated_data);
		    cb = {success: this.handleSuccess, failure: this.handleFailure}
		    Ext.lib.Ajax.request('POST', '${saveUrl}', cb, json_data);
		}},
	  %endif	
		{
        text: 'Add',
        handler : function(button){
			    grid = Ext.getCmp('${componentId}');
			    grid.stopEditing();
		        grid.store.handleAdd(grid);
	      	}
		}],
    onKeypress :	function (e){ 
	    e.preventDefault();
	    
	    var key = e.getKey()
	    
		switch (key) {
		    case 45:
	                this.stopEditing();
	                this.store.handleAdd(this);
		       		break
		}     
		
	    var target = e.getTarget();
	 },
	 afterRender : function(sender){
	 	this.store.load();
	 	this.on('keypress',this.onKeypress);
	 }
     
}
    